<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <div class="albums-manager">
        <el-dialog :fullscreen="false" width="800px" :lock-scroll="true" :visible="true" title="厂房分类管理"
            :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" v-if="show"
            @close="onClose">
            <div class="tools">
                <el-button @click="newHandle" icon="el-icon-plus" type="primary" size="small"
                    v-if="!showForm">创建一个分类</el-button>
            </div>
            <el-divider></el-divider>

            <el-table :data="albums" v-if="!showForm">
                <el-table-column label="ID" prop="id"> </el-table-column>
                <el-table-column label="分类名称" prop="name"> </el-table-column>
                <el-table-column label="厂房数" prop="post_count"> </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-link @cancle="() => showForm = false" @click="editHandle(scope.row)">编辑</el-link>
                        <el-link type="danger" @click="deleteHandle(scope.row.id)">删除</el-link>
                    </template>

                </el-table-column>

            </el-table>

            <album-form v-if="showForm" @change="changeHandle" :album="album" @cancle="() => showForm = false" />

        </el-dialog>
    </div>
</template>

<style scoped lang="scss">
.tools {
    display: flex;
    justify-content: flex-end;
}
</style>

<script>
import { getPostSimpleList, getAlbumList, getPostAlbumList, deleteAlbum } from "@/api/post";
import AlbumForm from "./albumForm";
import UdCard from "@/components/UdCard";
export default {
    components: { AlbumForm, UdCard },
    data () {
        return {
            show: false,
            showForm: false,
            albums: [],
            album: {},
            loading: false,
            currentAlbumId: 0,
        };
    },
    created: function () {
        this.loadData();
    },

    methods: {
        newHandle: function () {
            this.album = { name: '', key: '', content: '', post_ids: [], cover: '' }
            this.showForm = true
        },

        changeHandle: function () {
            this.showForm = false
            this.loadData()
        },


        openDialog: function () {
            this.show = true;
        },
        onClose: function () {
            this.show = false;
        },


        deleteHandle: function (aid) {
            var msg =
                "您确定要删除这个房源分类吗？（删除房源分类，不会删除系统房源数据）";
            this.$confirm(msg, "删除确认").then((resp) => {
                this.loading = true;
                deleteAlbum(aid).then((resp) => {
                    if (resp.status == 0) {
                        this.$message.success("已删除");
                        this.loadData()
                    }
                });
            });
        },
        editHandle: function (album) {
            this.album = album;
            this.showForm = true
        },
        loadData: function () {
            this.loading = true;
            this.loadAlbums();
        },
        // 加载系统已有分类
        loadAlbums: function () {
            getAlbumList().then((resp) => {
                this.albums = resp.data;
                this.loading = false;
            });
        },
    },
};
</script>
